package org.jeecg.modules.htxincailiao.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.report.entity.HTReport;
import org.jeecg.modules.htxincailiao.report.entity.HTReportCategory;
import org.jeecg.modules.htxincailiao.report.service.IHTReportCategoryService;
import org.jeecg.modules.htxincailiao.report.service.IHTReportService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: ht_report
* @Author: jeecg-boot
* @Date:   2022-07-14 10:29:31
* @Version: V1.0
*/
@Api(tags="ht_report")
@RestController
@RequestMapping("/report/hTReport")
@Slf4j
public class HTReportController extends JeecgController<HTReport, IHTReportService> {
   @Resource
   private IHTReportService hTReportService;

   @Resource
   private IHTReportCategoryService hTReportCategoryService;

   /**
    * 分页列表查询
    *
    * @param hTReport
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "ht_report-分页列表查询")
   @ApiOperation(value="ht_report-分页列表查询", notes="ht_report-分页列表查询")
   @RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
   public Result<?> queryPageList(HTReport hTReport,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {

       if (req.getMethod().equals("POST")){
           String line = RequestUtil.getPostParam(req);
           if (oConvertUtils.isNotEmpty(line)) {
               hTReport = JSONObject.parseObject(line, HTReport.class);
               pageNo = oConvertUtils.isNotEmpty(hTReport.getPageNo()) ? hTReport.getPageNo() : pageNo;
               pageSize = oConvertUtils.isNotEmpty(hTReport.getPageSize()) ? hTReport.getPageSize() : pageSize;
           }
       }
       if(oConvertUtils.isNotEmpty(hTReport) &&
               oConvertUtils.isNotEmpty(hTReport.getCategoryId()) &&
               hTReport.getCategoryId().equals("0")) {
           //全部分类
           hTReport.setCategoryId(null)	;
       }

       QueryWrapper<HTReport> queryWrapper = QueryGenerator.initQueryWrapper(hTReport, req.getParameterMap());
       queryWrapper.orderByDesc("update_time");

       Page<HTReport> page = new Page<HTReport>(pageNo, pageSize);
       IPage<HTReport> pageList = hTReportService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param hTReport
    * @return
    */
   @AutoLog(value = "ht_report-添加")
   @ApiOperation(value="ht_report-添加", notes="ht_report-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody HTReport hTReport) {
       hTReportService.save(hTReport);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param hTReport
    * @return
    */
   @AutoLog(value = "ht_report-编辑")
   @ApiOperation(value="ht_report-编辑", notes="ht_report-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody HTReport hTReport) {
       hTReportService.updateById(hTReport);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "ht_report-通过id删除")
   @ApiOperation(value="ht_report-通过id删除", notes="ht_report-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       hTReportService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "ht_report-批量删除")
   @ApiOperation(value="ht_report-批量删除", notes="ht_report-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.hTReportService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    * @param id
    * @return
    */
   @AutoLog(value = "ht_report-通过id查询")
   @ApiOperation(value="ht_report-通过id查询", notes="ht_report-通过id查询")
   @RequestMapping(value = "/queryById", method = {RequestMethod.GET,RequestMethod.POST})
   public Result<?> queryById(HttpServletRequest req, @RequestParam(name="id",required=false) String id) {
       if (req.getMethod().equals("POST")){
           String line = RequestUtil.getPostParam(req);
           if (oConvertUtils.isNotEmpty(line)) {
               HTReport hTReport  = JSONObject.parseObject(line, HTReport.class);
               id = hTReport.getId();
           }
       }

       HTReport hTReport = hTReportService.getById(id);
       if (oConvertUtils.isNotEmpty(hTReport) && oConvertUtils.isNotEmpty(hTReport.getCategoryId())){
           HTReportCategory category = hTReportCategoryService.getById(hTReport.getCategoryId());
           if (oConvertUtils.isNotEmpty(category) && oConvertUtils.isNotEmpty(category.getName())){
               hTReport.setCategoryName(category.getName());
           }
       }

       if(hTReport==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(hTReport);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param hTReport
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, HTReport hTReport) {
       return super.exportXls(request, hTReport, HTReport.class, "ht_report");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, HTReport.class);
   }

}
